package com.an.framework.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Servlet Filter implementation class ProcessTimeFilter
 */
@WebFilter("/*")
public class ProcessTimeFilter implements Filter {

	private static final Logger logging = LoggerFactory.getLogger(ProcessTimeFilter.class);

	public ProcessTimeFilter() {
		logging.info("处理时长过滤器，构造函数执行");
	}

	public void destroy() {
		logging.info("处理时长过滤器，销毁函数");
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		long startTime = System.currentTimeMillis();
		chain.doFilter(request, response);
		long endTime = System.currentTimeMillis();
		logging.info("处理时长过滤器，本次请求处理时间：" + ((endTime - startTime) / 1000) + "秒");

	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		logging.info("处理时长过滤器，初始化函数");

	}

}
